package com.yksj.healthtalk.ui.server;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.universalimageloader.core.DefaultConfigurationFactory;
import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.ServerCenterMainItemEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.setting.SettingQrCodeActivity;
import com.yksj.healthtalk.ui.views.CirclePageIndicator;
import com.yksj.healthtalk.utils.ToastUtil;
//服务中心主界面修改
public class ServerCenterMainUI extends BaseFragmentActivity implements OnClickListener {

	ViewPager mPager;
	private ViewPager mViewPager;
	private CirclePageIndicator mIndicator;
	private ImageLoader imageLoader;
	private String responseJson;
	private String mCommercialId;//商户id
	private String isDoctor;//10 是医疗  20非医疗
	private List<ServerCenterMainItemEntity> mainItemEntities;
	private TabsPagerAdapter adapter;
	private boolean isSave=false;//true 表示收藏  false 未收藏
	private DisplayMetrics displayMetrics;
	DisplayImageOptions mDisplayImageOptions;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.server_center_main_activity_layout);
		initView();
	}
	
	private void initView() {
		initTitle();
		displayMetrics = new DisplayMetrics();
		mDisplayImageOptions = DefaultConfigurationFactory.createServerDisplayImageOptions(this);
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		Bundle bundle = getIntent().getExtras();
		mCommercialId = bundle.getString("MERCHANT_ID");
		responseJson =bundle.getString("content");
		imageLoader = ImageLoader.getInstance();
		titleLeftBtn.setOnClickListener(this);
		mViewPager = (ViewPager)findViewById(R.id.myviewpager);
		mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setOnClickListener(this);
		findViewById(R.id.top_line).setVisibility(View.GONE);
		parseTo(responseJson);
	}


	protected void parseTo(String content) {
		try {
			
			JSONObject object = new JSONObject(content);
			isDoctor=object.optString("MERCHANT_SERVICE_TYPE");
			JSONArray array= object.getJSONArray("FUNCTION");
			
			ImageView logo=(ImageView) findViewById(R.id.logo);
			
			if(displayMetrics.heightPixels<1000){
				imageLoader.displayImage(object.optString("LOGO_ICON"),logo,DefaultConfigurationFactory.createServerDisplayImageOptions(this));
			}else if(displayMetrics.heightPixels>1400){
				imageLoader.displayImage(object.optString("LOGO_ICON_3X"),logo,DefaultConfigurationFactory.createServerDisplayImageOptions(this));
			}else{
				imageLoader.displayImage(object.optString("LOGO_ICON_2X"),logo,DefaultConfigurationFactory.createServerDisplayImageOptions(this));
			}
//			titleTextV.setText(object.optString("MERCHANT_NAME"));
			mainItemEntities = ServerCenterMainItemEntity.parseToObject(array);
			if(mainItemEntities==null)return ;
			if(object.optString("COLLECTIONFLAG").equals("1")){
				titleRightBtn2.setBackgroundResource(R.drawable.icon_wishlists_press);
				isSave=true;
			}else{
				isSave=false;
				titleRightBtn2.setBackgroundResource(R.drawable.icon_wishlists);
			}
			
			adapter = new TabsPagerAdapter();
			mViewPager.setAdapter(adapter);
			mIndicator.setViewPager(mViewPager);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 收藏
	 * @param isChecked
	 */
	private void onUpdatetCollection(final boolean isChecked) {
		titleRightBtn2.setClickable(false);
		HttpRestClient.doHttpServerCollect(isChecked,mCommercialId,SmartFoxClient.getLoginUserId(),new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				JSONObject object;
				try {
					object = new JSONObject(content);
					String string = object.getString("message");
					ToastUtil.showToastPanl(string );
//					if(0==object.getInt("code"))mCollectbox.setChecked(!isChecked);
					if(0!=object.getInt("code")){
						isSave=!isSave;
						if(isSave){
							titleRightBtn2.setBackgroundResource(R.drawable.icon_wishlists_press);
						}else{
							titleRightBtn2.setBackgroundResource(R.drawable.icon_wishlists);
						}
						
					}
				} catch (JSONException e) {
				}
				
			}
			@Override
			public void onFinish() {
				super.onFinish();
				titleRightBtn2.setClickable(true);
			}
		});
	}
	
	@Override
	public void onClick(View arg0) {
		Intent intent;
		switch (arg0.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://二维码
			onUpdatetCollection(!isSave);
			break;
		case 1://信息中心
			intent = new Intent(ServerCenterMainUI.this,ServerCenterHomeActivity.class);
			intent.putExtra("type", 1);//ServerCenterHomeFragment
			intent.putExtra("id", mCommercialId);
			intent.putExtra("title",(String) arg0.getTag());
			startActivity(intent);
			break;
		case 2:
			if(TextUtils.isEmpty(isDoctor))return;
			if("10".equals(isDoctor)){//医疗
				intent =  new Intent(ServerCenterMainUI.this,ServerCenterDoctorDepartmentList.class);
				intent.putExtra("id", mCommercialId);
				intent.putExtra("title",(String) arg0.getTag());
				startActivity(intent);
			}else{//非医疗
				intent = new Intent(ServerCenterMainUI.this,ServerCenterDoctorListActivity.class);
				intent.putExtra("id", mCommercialId);
				if("20".equals(isDoctor)){
				intent.putExtra("isDoc", true);}
				else{
					intent.putExtra("isDoc", false);
				}
				intent.putExtra("title",(String) arg0.getTag());
				startActivity(intent);
			}
			break;
		case 4://商品浏览
			intent = new Intent(ServerCenterMainUI.this,ServerCenterFreeShopReadActivity.class);
			intent.putExtra("id", mCommercialId);
			intent.putExtra("title",(String) arg0.getTag());
			startActivity(intent);
			break;
		case 5://话题
			if(TextUtils.isEmpty(isDoctor))return;
			intent = new Intent(ServerCenterMainUI.this,ServerCenterServerSalonActivity.class);
			intent.putExtra("id", mCommercialId);
			intent.putExtra("isDoc", "20".equals(isDoctor));
			intent.putExtra("title",(String) arg0.getTag());
			startActivity(intent);
			break;
		case 7://投诉建议
			intent = new Intent(ServerCenterMainUI.this,ServerCenterReportFragment.class);
			intent.putExtra("id", mCommercialId);
			intent.putExtra("title",(String) arg0.getTag());
			startActivity(intent);
			break;
		case 6://二维码
			intent =new Intent(ServerCenterMainUI.this,SettingQrCodeActivity.class);
			intent.putExtra("id", mCommercialId);
			intent.putExtra("title",(String) arg0.getTag());
			intent.putExtra("type",1);
			startActivity(intent);
			break;
		}
	}
	
	private class TabsPagerAdapter extends PagerAdapter{
		final List<View> views = new ArrayList<View>();
		public TabsPagerAdapter() {
		View v = getLayoutInflater().inflate(R.layout.server_center_fragment_one,null);
			int[] id=new int[]{R.id.sms,R.id.doc_service,R.id.doc_salon,R.id.jianyi,R.id.tousu,R.id.qrcode};
			int length=mainItemEntities.size();
			int[] idText=new int[]{R.id.smstext,R.id.doc_servicetext,R.id.doc_salontext,R.id.jianyitext,R.id.tousu_text,R.id.qr_text};			
			for (int i = 0; i <length ; i++) {
				ServerCenterMainItemEntity centerMainItemEntity = mainItemEntities.get(i);
				ImageView button=(ImageView) v.findViewById(id[i]);
				button.setVisibility(View.VISIBLE);
				button.setId(Integer.valueOf(centerMainItemEntity.getFUNCTION_ID()));
				button.setOnClickListener(ServerCenterMainUI.this);
				button.setTag(centerMainItemEntity.getDEFINED_NAME());
				String pathPic;
				if(displayMetrics.heightPixels<1000){
					 pathPic=centerMainItemEntity.getPICPATH();
				}else if(displayMetrics.heightPixels>1400){
					 pathPic=centerMainItemEntity.getAndroid_Icon();
				}else{
					pathPic=centerMainItemEntity.getSMALL_ANDROID_ICON_2X();
				}
				imageLoader.displayImage(pathPic, button,mDisplayImageOptions);
				 v.findViewById(idText[i]).setVisibility(View.GONE);
			}
			views.add(v);
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public int getCount() {
			return views.size();
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View item = views.get(position);
			container.addView(item);
			return item;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}
}
